package com.syj.collect.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.syj.collect.app.R;
import com.syj.collect.entry.TagAdapterData;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.MyTagViewHoder> {


    private static final String TAG = "tagadapter ";
    private List<TagAdapterData.WaterBean> mutilSelectedList = new ArrayList<>();
    private List<TagAdapterData.WaterBean> dList = new ArrayList<>();

    @NonNull
    @Override
    public TagAdapter.MyTagViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tv, parent, false);
        return new MyTagViewHoder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TagAdapter.MyTagViewHoder holder, int position) {
        holder.tv.setText(dList.get(position).getLabel());
        if (dList.get(position).isIscheckd()){
            holder.tv.setTextColor(holder.itemView.getResources().getColor(R.color.app_theme));

            holder.tv.setBackground(holder.itemView.getResources().getDrawable(R.drawable.tag_recnt_true));
        }else {
            holder.tv.setTextColor(holder.itemView.getResources().getColor(R.color.gray));
            holder.tv.setBackground(holder.itemView.getResources().getDrawable(R.drawable.tag_recnt_false));
        }


    /*    if (!mutilSelectedList.isEmpty() && mutilSelectedList.contains(dList.get(position))) {
            // holder.itemView.setBackground(R.drawable.tag_selsect);
            holder.tv.setTextColor(Color.BLUE);
            holder.tv.setBackground(holder.itemView.getResources().getDrawable(R.drawable.tag_recnt_true));
        } else {
            holder.tv.setTextColor(Color.GRAY);

            holder.tv.setBackground(holder.itemView.getResources().getDrawable(R.drawable.tag_recnt_false));
        }
*/
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mutilSelectedList.contains(dList.get(position))) {
                    mutilSelectedList.remove(dList.get(position));
                    // holder.ivSelect.isSelected = false
//                    Log.i(TAG, "onBindViewHolder: 取消选中");
                    holder.tv.setTextColor(holder.itemView.getResources().getColor(R.color.gray));
                    holder.tv.setBackground(holder.itemView.getResources().getDrawable(R.drawable.tag_recnt_false));
                } else {
                    mutilSelectedList.add(dList.get(position));
                    //holder.ivSelect.isSelected = true
//                    Log.i(TAG, "onBindViewHolder: 选中");
                    holder.tv.setTextColor(holder.itemView.getResources().getColor(R.color.app_theme));

                    holder.tv.setBackground(holder.itemView.getResources().getDrawable(R.drawable.tag_recnt_true));
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return dList.size();
    }

    public void upDate(List<TagAdapterData.WaterBean> tagl) {
        this.dList.clear();
        if (tagl.isEmpty()) return;
        dList.addAll(tagl);
        notifyDataSetChanged();
        //this.dList.addAll(tagl);

    }

    public List<TagAdapterData.WaterBean> getSelect() {
        return mutilSelectedList;
    }

    /**
     *
     */
    class MyTagViewHoder extends RecyclerView.ViewHolder {
        private final TextView tv;

        public MyTagViewHoder(@NonNull View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.item_tv_);
        }
    }
}
